package api.cie.rss;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class FileChooserActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.article_choose_file);
		initialize();
	}

	private void initialize() {
		final String xmlStr = getIntent().getExtras().getString("xml");		
		Button btnApply = (Button)findViewById(R.id.btnApply);
		btnApply.setOnClickListener(new OnClickListener() {			
			
			@Override
			public void onClick(View v) {
				EditText textFile = (EditText)findViewById(R.id.textFile);
				String fileName = textFile.getText().toString();
				FileOutputStream fos;
				try {
					fos = openFileOutput(fileName, Context.MODE_PRIVATE);
					fos.write(xmlStr.getBytes());
					fos.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				Intent intent = new Intent(FileChooserActivity.this, FavoriteListActivity.class);
				startActivity(intent);
			}
		});
		
		Button btnCancel = (Button)findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FileChooserActivity.this, MainActivity.class);
				startActivity(intent);
			}
		});
	}
	
}
